package com.sc.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.sc.common.pojo.CommonResult;
import com.sc.common.pojo.EasyUiDataGridResult;
import com.sc.mapper.TbContentMapper;
import com.sc.pojo.TbContent;
import com.sc.pojo.TbContentExample;
import com.sc.service.ContentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sc on 2017/10/19.
 */

@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentMapper tbContentMapper;

    public EasyUiDataGridResult getList(Long categoryId, Integer page, Integer rows) {
        PageHelper.startPage(page,rows);
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<TbContent>  tbContentList = tbContentMapper.selectByExample(example);
        PageInfo<TbContent> pageinfo  = new PageInfo<TbContent>(tbContentList);
        EasyUiDataGridResult result = new EasyUiDataGridResult();
        result.setTotal(pageinfo.getTotal());
        result.setRows(tbContentList);
        return result;
    }

    @Override
    public CommonResult save(TbContent tbContent) {
        tbContentMapper.insert(tbContent);

        return CommonResult.ok();
    }

    @Override
    public CommonResult update(TbContent tbContent) {
        tbContentMapper.updateByPrimaryKey(tbContent);
        return CommonResult.ok();
    }

    @Override
    public CommonResult delete(String ids) {
        if(ids.contains(",")){
            String[] idlist= ids.split(",");
            for(String id:idlist){
                Long i = Long.parseLong(id);
                tbContentMapper.deleteByPrimaryKey(i);
            }
        }else{
            Long id = Long.parseLong(ids);
            tbContentMapper.deleteByPrimaryKey(id);
        }
        return CommonResult.ok();
    }
}
